﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization.Json;
using System.Threading;

namespace Foundation
{
    public static class Serializer
    {
        public const string JsonExtension = ".json";

        public static readonly List<Type> KnownTypes = new List<Type>
        {
            typeof (Type),
            typeof (Dictionary<string, string>),
        };

        public static void SerializeDataContract(this object item, string file = null, Type type = null)
        {
            try
            {
                type = type ?? item.GetType();
                if (string.IsNullOrEmpty(file))
                    file = type.Name + JsonExtension;
                var serializer = new DataContractJsonSerializer(type, KnownTypes);
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var stream = store.OpenFile(file, FileMode.OpenOrCreate))
                    {
                        var currentCulture = Thread.CurrentThread.CurrentCulture;
                        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                        serializer.WriteObject(stream, item);
                        Thread.CurrentThread.CurrentCulture = currentCulture;
                    }
                }
            }
            catch
            {
                return;
            }
        }

        public static TItem DeserializeDataContract<TItem>(string file = null)
        {
            try
            {
                if (string.IsNullOrEmpty(file)) 
                    file = typeof (TItem).Name + JsonExtension;
                var serializer = new DataContractJsonSerializer(typeof(TItem), KnownTypes);
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var stream = store.OpenFile(file, FileMode.Open))
                    {
                        var currentCulture = Thread.CurrentThread.CurrentCulture;
                        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
                        try
                        {
                            var item = (TItem) serializer.ReadObject(stream);
                            return item;
                        }
                        catch (Exception)
                        {
                            stream.Dispose();
                            store.DeleteFile(file);
                            return default(TItem);
                        }
                        finally
                        {
                            Thread.CurrentThread.CurrentCulture = currentCulture;
                        }
                    }   
                }
            }
            catch
            {
                return default(TItem);
            }
        }
    }
}
